#include "chooselevelscene.h"
#include <thread>
#include <QMenuBar>
#include <QPainter>
#include <QLabel>
#include <QDebug>
#include "custombutton.h"
#include <QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
	this->resize(320, 588);
	this->setWindowIcon(QIcon(":/Coin0001.png"));
	this->setWindowTitle("翻金币");
	QMenuBar * bar = menuBar();
	setMenuBar(bar);
	QMenu * startMenu = bar->addMenu("开始");
	// 添加子项
	QAction * quit = startMenu->addAction("退出");
	// 点击退出
	connect(quit, &QAction::triggered, [=](){
		this->close();
	});
	// 选择关卡音效
	QSound * music_choose = new QSound(":/TapButtonSound.wav",this);
	QSound * music_back = new QSound(":/BackButtonSound.wav",this);
	// 返回按钮
	CustomButton * back = new CustomButton(":/BackButton.png",":/BackButtonSelected.png");
	back->setParent(this);
	back->move(this->width()-back->width(),this->height()-back->height());
	connect(back,&CustomButton::clicked, [=](){
		// 返回主场景
		music_back->play();
		emit this->back_scene();
	});
	// 设置关卡矩阵
	for (int i=0;i < 20; ++i)
	{
		CustomButton * checkpoint = new CustomButton(":/LevelIcon.png");
		checkpoint->setParent(this);
		checkpoint->move(25 + i%4 * 70, 130 + i/4 * 70);

		// 点击某个关卡
		connect(checkpoint, &QPushButton::clicked, [=](){
			QString str = QString("你选择的第 %1 关").arg(i+1);
			qDebug() << str;
			//点击播放音效
			music_choose->play();
			// 新建玩游戏场景，不要从第0关开始
			play = new PlayScene(i+1);
			// 隐藏此窗口显示下一个窗口
			this->hide();
			// 设置窗口在屏幕上的位置并显示
			play->setGeometry(this->geometry());
			play->show();

			// 监听返回按钮
			connect(play, &PlayScene::back_scene, this,[=](){
				this->setGeometry(play->geometry());
				delete play;
				play = nullptr;
				this->show();
			});
		});

		// 在按钮上显示文字
		QLabel * num = new QLabel;
		num->setParent(this);
		num->setFixedSize(checkpoint->width(),checkpoint->height());
		num->move(25 + i%4 * 70, 130 + i/4 * 70);
		num->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
		num->setText(QString::number(i+1));
		// 设置鼠标穿透，像似js的冒泡事件
		num->setAttribute(Qt::WA_TransparentForMouseEvents);
	}
}
void ChooseLevelScene::paintEvent(QPaintEvent *)
{
	QPainter paint(this);
	QPixmap pix;
	// 设置背景图片
	pix.load(":/OtherSceneBg.png");
	paint.drawPixmap(0,0,this->width(),this->height(),pix);

	// 设置标题图片
	pix.load(":/Title.png");
	paint.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);
}
